package groupb6.shopsystem.controller.customer;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.pojo.form.OrderIdForm;
import groupb6.shopsystem.pojo.form.ReturnReasonForm;
import groupb6.shopsystem.pojo.form.ReturnShipForm;
import groupb6.shopsystem.pojo.res.OrderProductRes;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import groupb6.shopsystem.service.CustomerOrderService;

import java.util.List;

@Api(tags = "14:订单(r2 未写完) ")
@ApiSupport(author = "zyliang")
@RestController
@RequestMapping("/customer")
public class CustomerOrderController {
    @Autowired
    private CustomerOrderService customerOrderService;

    @ApiOperation("查看订单")
    @GetMapping("/order")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "错误"),})
    @ResponseBody
    public UniversalResponse<List<OrderProductRes>> order() {
        try {
            List<OrderProductRes> order_productListListList = customerOrderService.getOrder_ProductListByBuyerId();
            return new UniversalResponse<>(1000, "成功", order_productListListList);
        } catch (RuntimeException e) {
            return new UniversalResponse<>(1001, e.getMessage());
        }
    }

    @ApiOperation("查看单个订单")
    @GetMapping("/orderOne") // check
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "错误"),})
    @ResponseBody
    public UniversalResponse<OrderProductRes> orderOne(Integer orderId) {
        try {
            OrderProductRes order_productListListList = customerOrderService.getOrder_ProductListByBuyerIdOne(orderId);
            return new UniversalResponse<>(1000, "成功", order_productListListList);
        } catch (RuntimeException e) {
            return new UniversalResponse<>(1001, e.getMessage());
        }
    }


    @ApiOperation("用户收货")
    @PostMapping("/receive")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "success"),
            @ApiResponse(code = 3000, message = "订单状态不匹配"),
            @ApiResponse(code = 2004, message = "用户权限不足")})
    @ResponseBody
    public UniversalResponse<?> receive(@RequestBody OrderIdForm orderId) {
        User currentUser = JWTUtil.getCurrentUser();
        return customerOrderService.userReceive(currentUser, orderId.getOrderId());
    }

    @ApiOperation("用户提交退货申请")
    @PostMapping("/returnOrderSubmit")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "错误"),})
    @ResponseBody
    public UniversalResponse<?> returnOrderCustomer(@RequestBody ReturnReasonForm returnReasonForm) {
        return customerOrderService.returnOrderCustomer(returnReasonForm.getOrderId(), returnReasonForm.getReason());
    }

    @ApiOperation("用户发货 填写物流单号")
    @PostMapping("/returnOrderShip")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "错误"),})
    @ResponseBody
    public UniversalResponse<?> returnOrderSeller(@RequestBody ReturnShipForm returnShipForm) {
        System.out.println(returnShipForm);
        return customerOrderService.returnOrderSeller(returnShipForm.getReturnId(), returnShipForm.getTrackingNumber());
    }
}
